package me.hys.service.system.impl;

import java.util.List;

import me.hys.dao.system.IConfigMapper;
import me.hys.entity.system.ConfigEntity;
import me.hys.kits.CollectionKit;
import me.hys.service.system.IConfigManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Author: @author HYS . <br>
 * Date: 2015-9-16 下午10:11:17 . <br>
 * Function: 配置项
 */
@Service
@Transactional
class ConfigManagerImpl implements IConfigManager {
	@Autowired
	private IConfigMapper mapper;

	/*
	 * (non-Javadoc)
	 * 
	 * @see me.hys.service.system.IConfigManager#findByFlag(java.lang.String)
	 */
	@Override
	public List<ConfigEntity> findByFlags(String[] flags) {
		return mapper.findByFlags(flags);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see me.hys.service.system.IConfigManager#findById(java.lang.String)
	 */
	@Override
	public ConfigEntity findById(String id) {
		return mapper.findById(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * me.hys.service.system.IConfigManager#findOneByFlags(java.lang.String[])
	 */
	@Override
	public ConfigEntity findOneByFlags(String[] flags) {
		List<ConfigEntity> list = findByFlags(flags);
		return CollectionKit.isNull(list) ? null : list.get(0);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * me.hys.service.system.IConfigManager#saveOrUpdate(me.hys.entity.system
	 * .ConfigEntity)
	 */
	@Override
	public int saveOrUpdate(ConfigEntity entity) {
		if (entity.getId() > 0) {
			return mapper.update(entity);
		} else {
			mapper.save(entity);
			return entity.getId();
		}
	}
}
